package com.zimi.controller;


import com.zimi.model.SysFunction;
import com.zimi.service.SysFunctionService;
import com.zimi.util.GsonUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统功能表 api
 *
 * @author lnintr
 * @since 2019-05-26
 */
@RestController
@RequestMapping("sys-function")
public class SysFunctionApi {
    @Autowired
    private SysFunctionService sysFunctionService;

    @PostMapping("list")
    @ApiOperation("查询功能信息")
    public Object selectFunctionList(SysFunction record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", sysFunctionService.getList(record));
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("add")
    @ApiOperation("添加功能信息")
    public Object add(SysFunction record) {
        if (record.getIconId() == null) {
            record.setIconId(0);
        }

        sysFunctionService.insert(record);
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }

    @PostMapping("update")
    @ApiOperation("保存功能信息")
    public Object update(HttpServletRequest request) {
        String[] ma = request.getParameterValues("functionList");

        List<SysFunction> sysFunctions = GsonUtil.json2Objs(ma[0], SysFunction.class);
        sysFunctionService.update(sysFunctions);
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        return objectMap;
    }
}
